package ynu.beggar.server.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import ynu.beggar.server.dao.BulletinDao;
import ynu.beggar.server.dao.ForumPostDao;
import ynu.beggar.server.entities.DO.ForumPostCreateDO;
import ynu.beggar.server.entities.DO.ForumPostDelDO;
import ynu.beggar.server.entities.DO.ForumPostSummaryDO;
import ynu.beggar.server.entities.DO.UserBasicInfoDO;
import ynu.beggar.server.entities.PO.BulletinPO;
import ynu.beggar.server.entities.PO.ForumPostPO;
import ynu.beggar.server.service.ForumService;
import ynu.beggar.server.utils.JsonData;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ForumServiceImpl implements ForumService {

    private ForumPostDao forumPostDao;
    private final static int IS_DEL = 1;
    private BulletinDao bulletinDao;

    @Autowired
    private void setForumPostDao(ForumPostDao forumPostDao) {
        this.forumPostDao = forumPostDao;
    }

    @Autowired
    private void setBulletinDao(BulletinDao bulletinDao) {
        this.bulletinDao = bulletinDao;
    }


    @Override
    public JsonData queryPostPage(Pageable pageable, String type) {
        Page<ForumPostPO> forumPostPage = forumPostDao.findAllByPostTypeAndDelIsNullOrDelNot(type, IS_DEL, pageable);
        List<ForumPostPO> forumPostList = forumPostPage.getContent();
        return JsonData.buildSuccess(forumPostList.stream().map(ForumPostSummaryDO::new).collect(Collectors.toList()));
    }

    @Override
    public JsonData createPost(ForumPostCreateDO forumPostCreateDO, UserBasicInfoDO userBasicInfoDO) {
        ForumPostPO forumPostPO = new ForumPostPO(forumPostCreateDO, userBasicInfoDO);
        forumPostDao.save(forumPostPO);
        return JsonData.buildSuccess("发帖成功");
    }

    @Override
    public JsonData deletePost(ForumPostDelDO forumPostDelDO, UserBasicInfoDO userBasicInfoDO) {
        int postId = forumPostDelDO.getPostId();
        int userId = userBasicInfoDO.getUserId();
        List<ForumPostPO> forumPostToDelList = forumPostDao.findByPostIdAndUserIdAndDelIsNullOrDelNot(postId, userId, IS_DEL);
        if (forumPostToDelList.size() == 0) {
            return JsonData.buildError("您没有权限删除该帖子或该贴已删除");
        }
        ForumPostPO forumPostToDel = forumPostToDelList.get(0);
        forumPostToDel.setDel(IS_DEL);
        forumPostDao.save(forumPostToDel);
        return JsonData.buildSuccess("删除成功");
    }

    @Override
    public JsonData queryMyPostPage(Pageable pageable, UserBasicInfoDO userBasicInfoDO) {
        List<ForumPostPO> forumPostList = forumPostDao.findAllByUserIdAndDelIsNullOrDelNot(userBasicInfoDO.getUserId(), IS_DEL, pageable);
        return JsonData.buildSuccess(forumPostList.stream().map(ForumPostSummaryDO::new).collect(Collectors.toList()));
    }

    @Override
    public JsonData queryPostDetail(int postId) {
        ForumPostPO forumPostPO = forumPostDao.findByPostId(postId);
        if (forumPostPO == null) {
            return JsonData.buildError("查询错误");
        }
        if (Objects.equals(forumPostPO.getDel(), IS_DEL)) {
            return JsonData.buildError("帖子被删除了");
        }
        return JsonData.buildSuccess(forumPostPO);
    }

    @Override
    public JsonData queryBulletins() {
        Iterable<BulletinPO> bulletins = bulletinDao.findAll();
        return JsonData.buildSuccess(bulletins);
    }
}
